package neusoft.edu.cn.guanshangshiguang.activities;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import neusoft.edu.cn.guanshangshiguang.Listener.ListListener;
import neusoft.edu.cn.guanshangshiguang.Listener.OnItemClickListener;
import neusoft.edu.cn.guanshangshiguang.R;
import neusoft.edu.cn.guanshangshiguang.adapter.SearchOnlineAdapter;
import neusoft.edu.cn.guanshangshiguang.adapter.SearchSchoolAdapter;
import neusoft.edu.cn.guanshangshiguang.bean.SearchOnlineBean;
import neusoft.edu.cn.guanshangshiguang.bean.SearchSchoolBean;
import neusoft.edu.cn.guanshangshiguang.model.SearchOnlineModel;
import neusoft.edu.cn.guanshangshiguang.model.SearchSchoolModel;

public class SearchActivity extends AppCompatActivity {
    private List<SearchSchoolBean> schoolBookListBeanList;
    SearchSchoolAdapter schoolBookListAdapter;

    private List<SearchOnlineBean> onlineBookListBeanList;
    SearchOnlineAdapter onlineBookListAdapter;

    RecyclerView recyclerView;

    private EditText textView;
    private Button online_search,school_search;
    private String bname;


    ListListener schoolBookListListener = new ListListener() {
        @Override
        public void onResponse(List list) {
            schoolBookListBeanList = list;
            schoolBookListAdapter.setDate(schoolBookListBeanList);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(), "失败："+msg, Toast.LENGTH_SHORT).show();
        }
    };

    ListListener onlineBookListListener = new ListListener() {
        @Override
        public void onResponse(List List) {
            onlineBookListBeanList = List;
            onlineBookListAdapter.setDate(onlineBookListBeanList);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(), "失败："+msg, Toast.LENGTH_SHORT).show();
        }
    };

    OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(getApplicationContext(),ArticleDetailsActivity.class);
            startActivity(intent);
            finish();

        }
    };

    OnItemClickListener itemClickListener1 = new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(getApplicationContext(),ArticleDetailsActivity.class);
            startActivity(intent);
            finish();

        }
    };

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();//去掉标题栏
        actionBar.hide();//去掉标题栏
        setContentView(R.layout.activity_search);

        recyclerView = findViewById(R.id.search_recycler);
        //布局管理器  可以设置显示数据的方式为垂直或者水平
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(linearLayoutManager);

        textView=findViewById(R.id.food_search_textview);
        online_search=findViewById(R.id.online_search);
        school_search=findViewById(R.id.school_search);


        school_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                schoolBookListAdapter = new SearchSchoolAdapter();
                schoolBookListAdapter.setOnItemClickListener(itemClickListener);
                recyclerView.setAdapter(schoolBookListAdapter);
                bname=textView.getText().toString();
                SearchSchoolModel searchSchoolModel = new SearchSchoolModel();
                searchSchoolModel.getResultList(bname,MainActivity.User_School,schoolBookListListener);
            }
        });

        online_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onlineBookListAdapter = new SearchOnlineAdapter();
                onlineBookListAdapter.setOnItemClickListener(itemClickListener1);
                recyclerView.setAdapter(onlineBookListAdapter);
                bname=textView.getText().toString();
                SearchOnlineModel searchOnlineModel = new SearchOnlineModel();
                searchOnlineModel.getResultList(bname,onlineBookListListener);
            }
        });



    }
}
